package midend.IR_Instruction;

import midend.IR_Value.IR_BaseBlock;
import midend.IR_Value.IR_FuncBlock;
import midend.IR_Value.IR_Value;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Ret_Instruction extends Instruction{

    public Ret_Instruction(List<IR_Value> op, IR_BaseBlock bb) {
        super(op, bb);
        addValue(op.get(0));
    }

    public void updateop(int index, IR_Value newValue) {
        if(index == 0){
            set_Op(index, newValue);
        }
    }
    @Override
    public String toString() {
        if(get_op().get(0) == null){
            return "ret void";
        }
        return "ret " + get_op().get(0).get_Type().get_TypeName() + " " + get_op().get(0).get_ValueName();
    }

    @Override
    public Ret_Instruction clone(IR_BaseBlock block) {
        ArrayList<IR_Value> copy_ops = new ArrayList<>();
        IR_Value res = get_op().get(0);
        if (IR_FuncBlock.cloneMap_Value.containsKey(res)) {
            res = IR_FuncBlock.cloneMap_Value.get(get_op().get(0));
        }
        copy_ops.add(res);
        Ret_Instruction retInstruction = new Ret_Instruction(copy_ops, block);
        return retInstruction;
    }
}
